package com.jxs.android.weixi;

import java.util.ArrayList;
import java.util.List;

import org.ksoap2.serialization.SoapObject;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.jxs.android.weixi.data.CustInfo;
import com.telecom.weixi.R;

public class MyCustomer extends MyCommnList {
	private CustInfoAdapter adapter;
	private List<CustInfo> resInfos = new ArrayList<CustInfo>();

	@Override
	public String getMethod() {
		return "getCustStat";
	}

	@Override
	public Object paserData(SoapObject taskBeanObject) {
		resInfos.clear();
		/**
		 * @id 0
		 * @serv_gz_num:公众类型用户数
		 * @serv_zq_num:政企类型用户数
		 * @serv_all_num:用户数
		 */
		CustInfo custInfo = new CustInfo();
		custInfo.id = 0;
		custInfo.title = "我的用户";
		custInfo.name_1 = "公众";
		custInfo.name_1_value = taskBeanObject.getPropertySafelyAsString("serv_gz_num");
		custInfo.name_2 = "政企";
		custInfo.name_2_value = taskBeanObject.getPropertySafelyAsString("serv_zq_num");
		custInfo.name_3 = "用户数";
		custInfo.name_3_value = taskBeanObject.getPropertySafelyAsString("serv_all_num");
		custInfo.name_4 = "离网用户数";
		custInfo.name_4_value = taskBeanObject.getPropertySafelyAsString("serv_lost_num");
		resInfos.add(custInfo);

		/**
		 * @id 1
		 * @cust_gz_num:公众类型客户数
		 * @cust_zq_num:政企类型客户数
		 * @cust_all_num:客户数
		 */
		custInfo = new CustInfo();
		custInfo.id = 1;
		custInfo.title = "我的客户";
		custInfo.name_1 = "公众";
		custInfo.name_1_value = taskBeanObject.getPropertySafelyAsString("cust_gz_num");
		custInfo.name_2 = "政企";
		custInfo.name_2_value = taskBeanObject.getPropertySafelyAsString("cust_zq_num");
		custInfo.name_3 = "客户数";
		custInfo.name_3_value = taskBeanObject.getPropertySafelyAsString("cust_all_num");
		resInfos.add(custInfo);

		return resInfos;
	}

	@SuppressWarnings("unchecked")
	@Override
	public void updateList(Object object) {
		adapter.setCustInfos((List<CustInfo>) object);
		adapter.notifyDataSetChanged();
	}

	@Override
	public OnItemClickListener getItemClickListener() {
		return new AdapterView.OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int location, long arg3) {
				Intent intent = new Intent();
				Bundle bundle = new Bundle();
				String title = "";
				CustInfo info = resInfos.get(location - 1);
				if (info.id == 0) {
					title = "我的用户";
					intent.setClass(MyCustomer.this, MyCustomer_User_Laucher.class);
				} else if (info.id == 1) {
					title = "我的客户";
					intent.setClass(MyCustomer.this, MyCustomer_Customer.class);
				}
				bundle.putString("title", title);
				bundle.putString("serv_all_num", info.name_3_value);
				bundle.putString("serv_lost_num", info.name_4_value);
				bundle.putString("cust_all_num", info.name_3_value);
				intent.putExtras(bundle);
				startActivity(intent);
			}
		};
	}

	@Override
	public BaseAdapter getAdapter(Context context) {
		adapter = new CustInfoAdapter(context);
		return adapter;
	}

	@Override
	public String getTopBarTitle() {
		return getString(R.string.home_cust);
	}

	class CustInfoAdapter extends BaseAdapter {

		List<CustInfo> custInfos = new ArrayList<CustInfo>();

		private LayoutInflater inflater = null;
		private ViewHolder holder = null;

		public CustInfoAdapter(Context context) {
			inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		}

		public void setCustInfos(List<CustInfo> custInfos) {
			this.custInfos.clear();
			this.custInfos.addAll(custInfos);
		}

		@Override
		public int getCount() {
			return custInfos.size();
		}

		@Override
		public CustInfo getItem(int position) {
			return custInfos.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			CustInfo custInfo = custInfos.get(position);

			if (convertView == null) {
				convertView = inflater.inflate(R.layout.bu_list_item, null);
				holder = new ViewHolder();
				holder.icon_iv = (ImageView) convertView.findViewById(R.id.item_icon);
				holder.title_tv = (TextView) convertView.findViewById(R.id.item_title);
				holder.value_tv = (TextView) convertView.findViewById(R.id.item_des);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			/*
			 * serv_gz_num:公众类型用户数 serv_zq_num:政企类型用户数 serv_all_num:用户数
			 * cust_gz_num:公众类型客户数 cust_zq_num:政企类型客户数 cust_all_num:客户数
			 */
			holder.title_tv.setText(custInfo.title);
			holder.title_tv.setTextColor(Color.rgb(0, 0, 0));
			holder.value_tv.setText(custInfo.name_3 + ":" + custInfo.name_3_value + "  " + custInfo.name_2 + ":" + custInfo.name_2_value + custInfo.name_1 + ":" + custInfo.name_1_value);
			return convertView;
		}

		class ViewHolder {
			ImageView icon_iv;
			TextView title_tv;
			TextView value_tv;
		}

	}

}
